﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LogManager.Base;
using LogManager.Base.Models.Projections;
using Raven.Client.Indexes;

namespace LogManager.LocalStorage.Indexes
{
    public class Events_PerLog : AbstractIndexCreationTask<Event, EventsPerLogProjection>
    {
        public Events_PerLog()
        {
            Map = events =>
                  from e in events
                  select new {LogName = e.LogName, Count = 1, LastWriteTime = e.TimeStamp};
            Reduce = events =>
                     from e in events
                     group e by e.LogName
                     into g
                     select new {LogName = g.Key, Count = g.Sum(x => x.Count), LastWriteTime = g.OrderBy(x => x.LastWriteTime).FirstOrDefault().LastWriteTime};
        }
    }
}
